<template>
	<view>
		<view id="_drag_button" class="drag" :style="'left: ' + data.left + 'px; top:' + data.top + 'px;'"
			@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
			@click.stop.prevent="click" :class="{transition: isDock && !data.isMove }">
			<u-icon :name="icon" color="#2979ff" size="42"></u-icon>
		</view>
	</view>
</template>

<script>
	import {
		reactive
	} from 'vue'
	export default {
		name: 'drag-button',
		props: {
			isDock: {
				type: Boolean,
				default: false
			},
			existTabBar: {
				type: Boolean,
				default: false
			},
			icon: {
				type: String,
				default: "plus-circle-fill"
			}
		},
		setup() {
			const data = reactive({
				top: 0,
				left: 0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10,
			})

			// 将 ref 暴露给模板
			return {
				data
			}
		},
		mounted() {
			const sys = uni.getSystemInfoSync();
			
			this.data.windowWidth = sys.windowWidth;
			this.data.windowHeight = sys.windowHeight;

			// #ifdef APP-PLUS
			this.existTabBar && (this.data.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.data.windowHeight += sys.windowTop;
			}

			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				this.data.width = data.width;
				this.data.height = data.height;
				this.data.offsetWidth = data.width / 2;
				this.data.offsetHeight = data.height / 2;
				this.data.left = this.data.windowWidth - this.data.width - this.data.edge +10;
				this.data.top = this.data.windowHeight - this.data.height - this.data.edge - 50;
			}).exec();
		},
		methods: {
			click() {
				this.$emit('btnClick');
			},
			touchstart(e) {
				this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}

				this.data.isMove = true;

				this.data.left = e.touches[0].clientX - this.data.offsetWidth;

				let clientY = e.touches[0].clientY - this.data.offsetHeight;
				// #ifdef H5
				clientY += this.data.height;
				// #endif
				let edgeBottom = this.data.windowHeight - this.data.height - this.data.edge;

				// 上下触及边界
				if (clientY < this.data.edge) {
					this.data.top = this.data.edge;
				} else if (clientY > edgeBottom) {
					this.data.top = edgeBottom;
				} else {
					this.data.top = clientY
				}

			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.data.windowWidth - this.data.width - this.data.edge;

					if (this.data.left < this.data.windowWidth / 2 - this.data.offsetWidth) {
						this.data.left = this.data.edge - 10;
					} else {
						this.data.left = edgeRigth +10;
					}

				}

				this.data.isMove = false;

				this.$emit('btnTouchend');
			},
		}
	}
</script>

<style lang="scss" scoped>
	.drag {
		display: flex;
		justify-content: center;
		align-items: center;
		// background-color: rgba(0, 0, 0, 0.5);
		// box-shadow: 0 0 6upx rgba(0, 0, 0, 0.4);
		color: $uni-text-color-inverse;
		width: 80upx;
		height: 80upx;
		border-radius: 50%;
		// font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 999999;

		&.transition {
			transition: left .3s ease, top .3s ease;
		}
	}
</style>